<template>
	<view>我的</view>
</template>
<script setup>
	import {
		onMounted,
		ref,
		computed
	} from 'vue';
	import {
		onLoad,
		onShow,
		onHide
	} from '@dcloudio/uni-app';
	import {
		GetUserInfoData,
		GetWalletInfo,
		GetUserInfoApi,
		QueryBusinesses
	} from '@/api/modules/mine';
	import {
		formatPrice,
		replaceUrl,
		copy
	} from '@/utils/index.js';
	import useStore from '@/stores';
	
	const isShowSwiper = ref(false)
	const {
		globalStore
	} = useStore();
	const userInfo = ref(globalStore.userInfo);

	const walletInfo = ref({});

	let swiperArr = ['https://static.tianwandailian.cn/static/server/newimage/swiper_player.png',
		'https://static.tianwandailian.cn/static/server/newimage/lucky_turntable.png',
	]

	const navList = ref([
		[{
				num: '0',
				name: '我的关注',
				path: '/pages_mine/interest_list/index'
			},
			{
				num: '0',
				name: '完单数量',
				path: 'none'
			},
			{
				num: '0',
				name: '验收时长(h)',
				path: 'none'
			},
			{
				num: '0',
				name: '仲裁率',
				path: 'none'
			}
		]
	]);
	/**
	 * 我的背包数据
	 */
	const myPakage = ref([{
			name: '我的背包',
			icon: '../../static/mine/wdbb.png',
			path: '/pages_mine/my_bag/index'
		},
		{
			name: '足迹',
			icon: '../../static/mine/footprint.png',
			path: '/pages_receive/footprint/index'
		},
		{
			name: '收藏',
			icon: '../../static/mine/collect.png',
			path: '/pages_receive/collect/index'
		},
		{
			name: '等级奖励',
			icon: '../../static/mine/djjl.png',
			path: '/pages_mine/level_rewards/index'
		},
		{
			name: '活动公告',
			icon: '../../static/mine/hdgg.png',
			path: '/pages_home/notice_list/index'
		}
	]);
	/**
	 * 我的服务数据
	 */
	const myServe = ref([{
			name: '账号申诉',
			icon: '../../static/mine/zhss.png',
			path: '/pages_mine/account_appeal/index'
		},
		{
			name: '新手教学',
			icon: '../../static/mine/xsjx.png',
			path: '/pages_mine/novice_teaching/index'
		},
		{
			name: '规则中心',
			icon: '../../static/mine/gzzx.png',
			path: '/pages/system/rule_center'
		},
		{
			name: '投诉建议',
			icon: '../../static/mine/tsjy.png',
			path: '/pages_mine/complaint/index'
		},
		{
			name: '成为商户',
			icon: '../../static/mine/shrz.png',
			path: '/pages_mine/commercial_apply/index'
		},
		{
			name: '免保证金',
			icon: '../../static/mine/mianya-icon.png',
			path: '/pages_mine/free_margin/index'
		},
		{
			name: '消息提醒',
			icon: '../../static/mine/mianya-icon.png',
			path: '/pages_mine/message_reminder/index',
		},{
			name: '自动抢单',
			icon: '../../static/mine/mianya-icon.png',
			path: '/pages_mine/auto_rob_order/index'
		}
	]);
	// 成为商户逻辑判断
	const shouldShowItem = (item) => {
		if (item.name !== '成为商户') {
			return true;
		}
		return globalStore?.userInfo?.isMerchant * 1 === 0;
	}

	const imgType = computed(() => (data) => {
		const imgList = ['../../static/mine/level_bronze_medal.png', '../../static/mine/level_middle.png',
			'../../static/mine/level_high.png'
		]
		return imgList[data - 1]
	})

	function getUserInfoData() {
		GetUserInfoData({
			userId: globalStore.userInfo?.id
		}).then((res) => {
			navList.value[0][0].num = res.data?.focusedNum;
			navList.value[0][1].num = res.data?.completeOrderNum;
			navList.value[0][2].num = res.data?.acceptanceDuration;
			navList.value[0][3].num = res.data?.arbitrationRate;
		});
	}
	const getWalletInfo = async () => {
		const {
			data
		} = await GetWalletInfo();
		walletInfo.value = data;
	};

	function getUserInfo() {
		GetUserInfoApi().then((res) => {
			userInfo.value = res.data;
			globalStore.setUserInfo({
				...globalStore.userInfo,
				nickName: res.data?.nickName,
				endTime: res.data?.endTime,
				isMember: res.data?.isMember,
				isMerchant: res.data.isMerchant,
				isCertificate: res.data.isCertificate,
				levle: res.data.level
			});
		});
	}

	const handleClick = (index) => {
		if (index == 0) {
			if (!globalStore.token) {
				uni.$showModal({
					title: '',
					content: '暂未登录，请先登录!',
					confirmVal: '确定',
					cancelVal: '取消',
					success: (res) => {
						if (res.confirm) {
							uni.navigateTo({
								url: '/pages/login/index'
							});
						}
					}
				})
				return
			}
			uni.navigateTo({
				url: '/pages_mine/player_qualification/index'
			})
		} else {
			uni.navigateTo({
				url: '/pages_home/lucky_turntable/index'
			})
		}
	}
	//	防止请求多次调用
	const onShowFlag = ref(0);

	onMounted(() => {
		// #ifdef MP-WEIXIN
		setTimeout(() => {
			isShowSwiper.value = true
		}, 100)
		if (onShowFlag.value === 0 && globalStore.tabbarIdx === 5 && globalStore.token) {
			onShowFlag.value += 1;
			getUserInfoData();
			getWalletInfo();
			getUserInfo();

		}
		// #endif
	});


	onShow(() => {
		setTimeout(() => {
			isShowSwiper.value = true
		}, 100)
		if (onShowFlag.value === 0 && globalStore.tabbarIdx === 5 && globalStore.token) {
			onShowFlag.value += 1;
			getUserInfoData();
			getWalletInfo();
			getUserInfo();
		}

	});

	async function navigateTo(url) {
		const arr = ['/pages_mine/novice_teaching/index', '/pages/system/rule_center', '/pages/login/index'];
		//如果不等于这两个url才提示
		if (!globalStore.token && arr.every((item) => item !== url)) {
			uni.$showModal({
				title: '',
				content: '暂未登录，请先登录!',
				confirmVal: '确定',
				cancelVal: '取消'
			}).then(() => {
				uni.navigateTo({
					url: '/pages/login/index'
				});
			});
			onShowFlag.value = 0;
			return;
		}
	
		
		// 
		if (url === 'none') {
			return;
		}
		
		// 成为商户
		if (url === '/pages_mine/commercial_apply/index') {
			QueryBusinesses({
				userId: globalStore.userInfo?.id
			}).then((res) => {
				if (res.data === 500) {
					uni.$uv.toast('后台审核中，请耐心等待');
					return;
				}
			})
		}
		
		// 是免保证金并且未实名
		if (url === '/pages_mine/free_margin/index' && globalStore.userInfo?.isCertificate != 1) {
			uni.$showModal({
				title: '',
				content: '暂未实名，请先实名!',
				confirmVal: '确定',
				cancelVal: '取消'
			}).then(() => {
				uni.navigateTo({
					url: '/pages/system/certification'
				});
			})
			onShowFlag.value = 0;
			return;
		}
		
		uni.navigateTo({
			url
		});
		
		onShowFlag.value = 0
	}

	function goDatum() {
		if (globalStore.token) {
			uni.navigateTo({
				url: '/pages/system/personal_data'
			});
			onShowFlag.value = 0;
		}
	}
	/**
	 * 打开客服
	 */
	async function openCustomerService() {
		try {
			const res = await uni.openCustomerServiceConversation({
				sessionFrom: {
					nickName: 'aaa',
					avatarUrl: 'https://res.wx.qq.com/mpres/en_US/htmledition/comm_htmledition/images/icon/login/mp_app6cc018.png',
					groupId: '485285569'
				},
				showMessageCard: true
			})
			console.log('客服会话打开成功:', res)
		} catch (err) {
			console.error('客服功能异常:', err)
			uni.showToast({
				title: '客服功能暂不可用',
				icon: 'none'
			})
		}
	}
</script>

<style scoped lang="scss">
	@import '@/styles/mine.scss';

	.setting-box {
		width: 76rpx;
		height: 76rpx;
		position: fixed;
		bottom: 500rpx;
		right: 20rpx;
		z-index: 21;
		background-color: #fff;
		border: 1rpx solid #f5f5f5;
		box-shadow: 0 4rpx rgba(0, 0, 0, 0.1);
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;

		.wx-setting-img {
			width: 48rpx;
			height: 48rpx;
		}

	}

	.customer-box {
		width: 76rpx;
		height: 76rpx;
		position: fixed;
		bottom: 600rpx;
		right: 20rpx;
		z-index: 21;
		background-color: #fff;
		border: 1rpx solid #f5f5f5;
		box-shadow: 0 4rpx rgba(0, 0, 0, 0.1);
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;

		.wx-customer-serve {
			width: 48rpx;
			height: 48rpx;

		}
	}


	.wx-kefuBtn {
		position: absolute;
		width: 100rpx;
		background: transparent;
		bottom: 576rpx;
		right: 7rpx;
		z-index: 22;
	}

	.wx-kefuBtn::after {
		border: none !important;
	}
</style>